package com.shsoft.myThread;

import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.common.Constant;

import java.util.HashMap;

/**
 * 纯色-流水灯 子线程
 */
public class SingleFlowingWaterThread extends Thread{
    private MainActivity activity;
    private int index;
    private int bright; // 记录亮度
    private int direction; // 方向
    private int mode; // 模式
    private int speed; // 移动速度
    private int circle; // 圈数
    private int currentColorNum;  // 记录当前已经显示的预设颜色的编号
    private int[] rgb; // 记录当前显示的颜色
    private int currentCircle; // 记录当前的圈数
    public SingleFlowingWaterThread(MainActivity activity) {
        this.activity = activity;
        index = 0;
        currentColorNum = 0;
        currentCircle = 0;
        bright = Integer.parseInt(activity.flowingWater_bright.getText().toString());
        if(bright * 2 > 255){
            bright = 255;
        }else {
            bright = bright * 2;
        }
        direction = activity.flowingWater_direction_value;
        mode = activity.flowingWaterMode;
        speed = Integer.parseInt(activity.flowingWater_speed.getText().toString()) * 10;
        circle = Integer.parseInt(activity.flowingWater_circle.getText().toString());
    }

    @Override
    public void run() {
        System.out.println("纯色流水灯子线程运行");
        // 因为只写入若干个灯珠的数据，防止其他灯珠显示乱七八糟的的颜色，所以初始化 lights_4display
        activity.lights_4display = new HashMap<>();
        for(int i = 0; i < Constant.LIGHT_COUNT ; i++){
            activity.lights_4display.put(i,new int[]{1,1,1});
        }
        while (activity.isPlaying){
            if(index == 0 && currentCircle == 0){ // 换色
                rgb = new int[]{activity.colors.get(currentColorNum)[0],activity.colors.get(currentColorNum)[01],activity.colors.get(currentColorNum)[2]};
            }
            // 让主线程刷新灯珠区域
            activity.alpha = bright;
            activity.refreshLightsDiv();
            if(direction == Constant.CLOCKWISE) { // 顺时针
                if(mode == Constant.FLOWINGWATER_MODE_TAIL_FOLLOW){
                    if(index < 20){
                        activity.lights_4display.put(index, rgb);
                    }else {
                        activity.lights_4display.put((index - 20),new int[]{1,1,1});
                    }
                }else if(mode == Constant.FLOWINGWATER_MODE_HEAD_BACK){
                    if(index < 20){
                        activity.lights_4display.put(index, rgb);
                    }else {
                        activity.lights_4display.put(19 - (index - 20),new int[]{1,1,1});
                    }
                }else if(mode == Constant.FLOWINGWATER_MODE_FULL_FINISH){
                    if(index == 0){
                        for(int i = 0; i <Constant.LIGHT_COUNT ; i++){
                            activity.lights_4display.put(i,new int[]{1,1,1});
                        }
                    }
                    activity.lights_4display.put(index, rgb);
                    index++;
                    if(index == 20){
                        index = 0;
                        currentCircle++;
                        if(currentCircle == circle){
                            currentCircle = 0;
                            currentColorNum++;
                            if(currentColorNum == activity.colors.size()){
                                currentColorNum = 0;
                            }
                        }
                    }
                    try {
                        sleep(speed);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                    continue;
                }
            }else { // 逆时针
                if(mode == Constant.FLOWINGWATER_MODE_TAIL_FOLLOW){
                    if(index < 20){
                        if(index == 0){
                            activity.lights_4display.put(0, rgb);
                        }else {
                            activity.lights_4display.put((20 - index),rgb);
                        }
                    }else {
                        if(index == 20){
                            activity.lights_4display.put(0, new int[]{1,1,1});
                        }else {
                            activity.lights_4display.put((40 - index),new int[]{1,1,1});
                        }
                    }
                }else if(mode == Constant.FLOWINGWATER_MODE_HEAD_BACK){
                    if(index < 20){
                        if(index == 0){
                            activity.lights_4display.put(0, rgb);
                        }else {
                            activity.lights_4display.put((20 - index),rgb);
                        }
                    }else {
                        if(index == 39){
                            activity.lights_4display.put(0, new int[]{1,1,1});
                        }else {
                            activity.lights_4display.put(index - 19, new int[]{1, 1, 1});
                        }
                    }
                }else if(mode == Constant.FLOWINGWATER_MODE_FULL_FINISH){
                    if(index == 0){
                        for(int i = 0; i <Constant.LIGHT_COUNT ; i++){
                            activity.lights_4display.put(i,new int[]{1,1,1});
                        }
                        activity.lights_4display.put(0, rgb);
                    }else {
                        activity.lights_4display.put((20 - index),rgb);
                    }
                    index++;
                    if(index == 20){
                        index = 0;
                        currentCircle++;
                        if(currentCircle == circle){
                            currentCircle = 0;
                            currentColorNum++;
                            if(currentColorNum == activity.colors.size()){
                                currentColorNum = 0;
                            }
                        }
                    }
                    try {
                        sleep(speed);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                    continue;
                }
            }
            index++;
            if(index == 40){
                index = 0;
                currentCircle++;
                if(currentCircle == circle){
                    currentCircle = 0;
                    currentColorNum++;
                    if(currentColorNum == activity.colors.size()){
                        currentColorNum = 0;
                    }
                }
            }
            try {
                sleep(speed);
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }
    }
}
